package icu.sehnen.fitness.controller;



import icu.sehnen.fitness.common.CommonResult;
import icu.sehnen.fitness.entity.User;
import icu.sehnen.fitness.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sehnen
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${jwt.tokenHead}")
    String tokenHead;
    @Autowired
    IUserService userService;
    //登录
    @PostMapping("/login")
    public Object login( User user){
        HashMap<String, String> data = new HashMap<>();
        String token = null;
        try {
            token = userService.login(user);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.validateFailed("用户名或密码错误");
        }
        if (StringUtils.isEmpty(token)){
            return CommonResult.validateFailed("用户名或密码错误");
        }
        data.put("tokenHead",tokenHead);
        data.put("access_token",token);
        // localStorage.setItem("Authorization","Bearer sdsdfdfds")
        // $ajax{data:{},type:"",header:{"Authorization":"Bearer sdsdfdfds"}}
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('/wx/product/index')")
    @GetMapping("/getMessage")
    public CommonResult getMessage(){
        return CommonResult.success(userService.getPermissionsByUserId(2l));
    }



}
